package com.inputabc.ct.v1.service.impl;

import com.inputabc.EzGUIFramework.listener.ListenerBinder;
import com.inputabc.EzGUIFramework.util.alias.Ez;
import com.inputabc.ct.v1.context.Components;
import com.inputabc.ct.v1.context.ComponentsBuilder;
import com.inputabc.ct.v1.context.ContextParams;
import com.inputabc.ct.v1.service.InputBoxUIService;
import com.inputabc.ct.v1.ui.TextBox;
import com.inputabc.ct.v1.ui.inputbox.InputBoxBuilder;
import com.inputabc.ct.v1.ui.inputbox.action.InputBoxAction;
import com.inputabc.ct.v1.ui.inputbox.component.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.geom.RoundRectangle2D;

public class InputBoxUIServiceImpl implements InputBoxUIService {
    private Components textBoxComponents = ComponentsBuilder.getComponentsContext().get(TextBox.class);
    private JScrollPane jsp = (JScrollPane) textBoxComponents.get("textBoxScrollPane");
    private InputBoxAction inputBoxAction = new InputBoxAction();
    private InputBoxCloseButtonFrame inputBoxCloseButtonFrame =
            ((InputBoxBuilder) ContextParams.contextParam.get("inputBoxBuilder")).getInputBoxCloseButtonFrame();
    
    // 定义美化配色方案
    private static final Color PRIMARY_COLOR = new Color(40, 167, 226);  // 主色调
    private static final Color BG_COLOR = new Color(250, 250, 250);      // 背景色
    private static final Color TEXT_COLOR = new Color(40, 44, 52);       // 文字颜色
    private static final Color CARET_COLOR = new Color(40, 167, 226);    // 光标颜色
    private static final Color SELECTION_BG = new Color(85, 185, 228);   // 选中背景色
    private static final Color SELECTION_FG = Color.WHITE;               // 选中文字颜色
    private static final Color CLOSE_BUTTON_BG = new Color(228, 78, 78); // 关闭按钮背景色
    
    // 定义圆角大小
    private static final int CORNER_RADIUS = 15;

    @Override
    public void setBounds(InputBoxFrame inputBoxFrame) {
        inputBoxFrame.setSize(350, 120);
        Ez.center(inputBoxFrame, true);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        inputBoxFrame.setLocation(inputBoxFrame.getX(), (int) (screenSize.getHeight() - inputBoxFrame.getHeight() - 30));

        // 设置圆角窗口 - 使用现代API
        try {
            // 设置窗口为无装饰，这是使用形状的前提
            inputBoxFrame.setUndecorated(true);
            // 设置窗口形状
            inputBoxFrame.setShape(new RoundRectangle2D.Double(0, 0, inputBoxFrame.getWidth(), 
                    inputBoxFrame.getHeight(), CORNER_RADIUS, CORNER_RADIUS));
            
            // 设置窗口透明度
            inputBoxFrame.setOpacity(0.95f);
        } catch (Exception e) {
            // 如果设置失败，回退到普通窗口
            System.err.println("Failed to set window shape or opacity: " + e.getMessage());
        }

        // 配置关闭按钮窗口
        inputBoxCloseButtonFrame.setSize(32, 32);
        Ez.center(inputBoxCloseButtonFrame);
        
        try {
            // 设置关闭按钮窗口为无装饰
            inputBoxCloseButtonFrame.setUndecorated(true);
            // 设置关闭按钮窗口形状
            inputBoxCloseButtonFrame.setShape(new RoundRectangle2D.Double(0, 0, 
                    inputBoxCloseButtonFrame.getWidth(), inputBoxCloseButtonFrame.getHeight(), 32, 32));
        } catch (Exception e) {
            System.err.println("Failed to set close button window shape: " + e.getMessage());
        }
        
        // 设置关闭按钮位置在输入框的右上角
        int closeButtonX = inputBoxFrame.getX() + inputBoxFrame.getWidth() - 16;
        int closeButtonY = inputBoxFrame.getY() - 16;
        inputBoxCloseButtonFrame.setLocation(closeButtonX, closeButtonY);
    }

    @Override
    public void setLayouts(InputBoxFrame inputBoxFrame) {
        // 设置主面板布局
        InputBoxMainPanel inputBoxMainPanel = inputBoxFrame.getInputBoxMainPanel();
        inputBoxMainPanel.setLayout(new BorderLayout(10, 10));
        inputBoxMainPanel.setBorder(new EmptyBorder(12, 15, 12, 15));
        
        // 设置文本区域
        InputBoxTextArea inputBoxTextArea = inputBoxMainPanel.getInputBoxScrollPane().getInputBoxTextArea();
        inputBoxTextArea.setLayout(null);
        
        // 设置关闭按钮面板
        InputBoxCloseButtonPanel inputBoxCloseButtonPanel = inputBoxCloseButtonFrame.getInputBoxCloseButtonPanel();
        inputBoxCloseButtonPanel.setLayout(new GridBagLayout());
    }

    @Override
    public void setBackgrounds(InputBoxFrame inputBoxFrame) {
        // 设置主窗口背景
        inputBoxFrame.getContentPane().setBackground(BG_COLOR);
        InputBoxMainPanel mainPanel = inputBoxFrame.getInputBoxMainPanel();
        mainPanel.setBackground(BG_COLOR);
        
        // 设置滚动面板和文本区域背景
        InputBoxScrollPane scrollPane = mainPanel.getInputBoxScrollPane();
        scrollPane.setBackground(BG_COLOR);
        scrollPane.getViewport().setBackground(BG_COLOR);
        scrollPane.setBorder(BorderFactory.createEmptyBorder());
        
        // 设置文本区域背景
        InputBoxTextArea textArea = scrollPane.getInputBoxTextArea();
        textArea.setBackground(BG_COLOR);
        
        // 设置关闭按钮面板和背景
        InputBoxCloseButtonPanel closeButtonPanel = inputBoxCloseButtonFrame.getInputBoxCloseButtonPanel();
        closeButtonPanel.setBackground(CLOSE_BUTTON_BG);
        inputBoxCloseButtonFrame.setBackground(CLOSE_BUTTON_BG);
    }

    @Override
    public void setBorders(InputBoxFrame inputBoxFrame) {
        // 添加输入框主窗口的边框效果
        InputBoxMainPanel mainPanel = inputBoxFrame.getInputBoxMainPanel();
        mainPanel.setBorder(new EmptyBorder(12, 15, 12, 15));
        
        // 设置滚动面板边框
        InputBoxScrollPane scrollPane = mainPanel.getInputBoxScrollPane();
        scrollPane.setBorder(BorderFactory.createCompoundBorder(
                new LineBorder(new Color(230, 230, 230), 1, true),
                new EmptyBorder(8, 8, 8, 8)
        ));
    }

    @Override
    public void bindingListeners(InputBoxFrame inputBoxFrame) {
        InputBoxTextArea inputBoxTextArea = inputBoxFrame.getInputBoxMainPanel().getInputBoxScrollPane().getInputBoxTextArea();
        InputBoxPopupMenu inputBoxPopupMenu = inputBoxTextArea.getInputBoxPopupMenu();
        InputBoxCopyMenuItem inputBoxCopyMenuItem = inputBoxPopupMenu.getInputBoxCopyMenuItem();
        InputBoxCutMenuItem inputBoxCutMenuItem = inputBoxPopupMenu.getInputBoxCutMenuItem();
        InputBoxPasteMenuItem inputBoxPasteMenuItem = inputBoxPopupMenu.getInputBoxPasteMenuItem();

        // 绑定各种事件监听器
        ListenerBinder inputBoxListenerBinder = Ez.getListenerBinder(inputBoxAction);
        inputBoxListenerBinder.actionPerformed(inputBoxCopyMenuItem, "copy", inputBoxTextArea);
        inputBoxListenerBinder.actionPerformed(inputBoxCutMenuItem, "cut", inputBoxTextArea);
        inputBoxListenerBinder.actionPerformed(inputBoxPasteMenuItem, "paste", inputBoxTextArea);
        inputBoxListenerBinder.keyPressed(inputBoxTextArea, "enterDown");
        inputBoxListenerBinder.windowClosing(inputBoxFrame, "closing");
        inputBoxListenerBinder.windowOpened(inputBoxFrame, "opening");
        inputBoxListenerBinder.windowClosing(inputBoxCloseButtonFrame, "closing");
        inputBoxListenerBinder.windowOpened(inputBoxCloseButtonFrame, "opening");
        inputBoxListenerBinder.mouseClicked(inputBoxCloseButtonFrame.getInputBoxCloseButtonPanel().getIconLabel(), "pushCloseButton");
    }

    @Override
    public void setFonts(InputBoxFrame inputBoxFrame) {
        // 设置更美观的字体
        InputBoxTextArea inputBoxTextArea = inputBoxFrame.getInputBoxMainPanel().getInputBoxScrollPane().getInputBoxTextArea();
        
        // 尝试使用系统上更现代的字体，回退到默认等宽字体
        Font modernFont = null;
        String[] preferredFonts = {
            "Microsoft YaHei", "微软雅黑", "Source Han Sans CN", "思源黑体", 
            "Noto Sans CJK SC", "Segoe UI", "Tahoma", "Arial"
        };
        
        for (String fontName : preferredFonts) {
            try {
                modernFont = new Font(fontName, Font.PLAIN, 14);
                if (!modernFont.getFamily().equals("Dialog")) {
                    break;
                }
            } catch (Exception e) {
                // 忽略不可用的字体
            }
        }
        
        if (modernFont == null) {
            modernFont = new Font(Font.MONOSPACED, Font.PLAIN, 14);
        }
        
        inputBoxTextArea.setFont(modernFont);
    }

    @Override
    public void setOthers(InputBoxFrame inputBoxFrame) {
        // 设置文本区域的各种属性
        InputBoxTextArea inputBoxTextArea = inputBoxFrame.getInputBoxMainPanel().getInputBoxScrollPane().getInputBoxTextArea();
        inputBoxTextArea.setForeground(TEXT_COLOR);
        inputBoxTextArea.setSelectedTextColor(SELECTION_FG);
        inputBoxTextArea.setSelectionColor(SELECTION_BG);
        inputBoxTextArea.setCaretColor(CARET_COLOR);
        
        // 设置输入区提示文本
        inputBoxTextArea.setText("在此输入文本...");
        inputBoxTextArea.selectAll();
        
        // 自定义关闭按钮图标
        JLabel iconLabel = inputBoxCloseButtonFrame.getInputBoxCloseButtonPanel().getIconLabel();
        
        // 尝试加载自定义图标，如果失败则使用文本符号
        ImageIcon closeIcon = null;
        try {
            closeIcon = new ImageIcon(Toolkit.getDefaultToolkit()
                    .getImage(ContextParams.class.getResource("/icon/close_white.png")));
            
            // 如果图标加载失败，尝试使用原始图标
            if (closeIcon.getIconWidth() <= 0) {
                closeIcon = new ImageIcon(Toolkit.getDefaultToolkit()
                        .getImage(ContextParams.class.getResource("/icon/close_g.png")));
            }
            
            iconLabel.setIcon(closeIcon);
        } catch (Exception e) {
            // 如果图标加载失败，使用 "×" 文本作为替代
            iconLabel.setText("×");
            iconLabel.setFont(new Font("Arial", Font.BOLD, 18));
            iconLabel.setForeground(Color.WHITE);
            iconLabel.setHorizontalAlignment(SwingConstants.CENTER);
        }
        
        iconLabel.setBackground(CLOSE_BUTTON_BG);
        iconLabel.setBounds(0, 0, 32, 32);
        
        // 为关闭按钮添加悬停效果
        iconLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
    }
}
